package org.smoozik.services.sound;

import java.util.ArrayList;

import javax.sound.midi.Instrument;
import javax.swing.JComboBox;

import org.smoozik.services.musictheory.Chord;
import org.smoozik.services.musictheory.Token;
import org.smoozik.services.musictheory.Figure;
import org.smoozik.services.musictheory.Note;

public class SoundPlayer {

	ArrayList<SoundManager> sms;
	Synthe synth;
	int channel;

	public SoundPlayer() {
		this.sms = new ArrayList<>();
		synth = new Synthe();
		channel = 0;
	}

	public void playNote(int index) {
		// SoundManager sm = new SoundManager();
		// sm.playNoteInNewThread(index);
		//
		// this.sms.add(sm);
		synth.play(channel, 60 + index, 128, 2000);

	}

	public int getChannel() {
		return channel;
	}

	public void setChannel(int channel) {
		this.channel = channel;
	}

	public void playTick() {
		synth.play(9, 76, 60, 0);
	}

	public void playTock() {
		synth.play(9, 77, 60, 0);
	}

	public void stopNotes() {
		if (this.sms != null) {
			for (SoundManager t : this.sms) {
				t.setStopped(true);
			}
		}
	}

	public static void main(String[] args) throws InterruptedException {
		SoundPlayer sp = new SoundPlayer();

		sp.playNote(0);
		Thread.sleep(50);

		sp.stopNotes();
		System.out.println("stopped");

	}

	/**
	 * Plays a sound according to the currentFigure, the last generated figure
	 */
	public void playFigure(Figure f) {

		if (f instanceof Note) {
			Note note = (Note) f;
			playNote(note.getHauteur());
		} else if (f instanceof Chord) {
			Chord chord = (Chord) f;
			for (Note note : chord.getNotes()) {
				playNote(note.getHauteur());
			}
		}
	}

	/**
	 * Plays a sound according to the currentFigure, the last generated figure
	 */
	public void playToken(Token e, int timeWeight) {

		Figure f = e.getFigure();

		if (f instanceof Note) {
			Note note = (Note) f;
			playNote(e.getChannel(),note.getHauteur(), e.getVelocity(),
					(int) (timeWeight * e.getDuration()));
		} else if (f instanceof Chord) {
			Chord chord = (Chord) f;
			for (Note note : chord.getNotes()) {
				playNote(e.getChannel(),note.getHauteur(), e.getVelocity(),
						(int) (timeWeight * e.getDuration()));
			}
		}
	}

	public void playNote(int channel, int index, int velocity, int duration) {
		// SoundManager sm = new SoundManager();
		// sm.playNoteInNewThread(index);
		//
		// this.sms.add(sm);
		synth.play(channel, 60 + index, velocity, duration);
		// channel ++;
		// if(channel == 2) {
		// channel = 0;
		// }
	}

	public void loadComboBox(JComboBox<Instrument> combo) {
		Instrument[] instruments = synth.getInstrumentsList();

		if (instruments != null) {
			for (int j = instruments.length - 1; j > 0; j--) {
				// System.out.println(j + " - '"
				// + instrumentsList[j].getName().trim() + "'");
				// liste.put(instrumentsList[j].getName().trim(),
				// instrumentsList[j]);
				//
				combo.addItem(instruments[j]);
			}
		}
	}

	public void setInstrument(int channel, Instrument i) {
		synth.program(channel, i);
	}
	

	public Instrument getInstrument(int channel) {
		return synth.getInstrumentByChannel(channel);
	}
}
